<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;
use think\Controller;
use think\Db;
use app\common\model\UserAccount;
use app\common\model\UserAccountLog;

class Wallet extends Controller
{
	//取不重复助记词 递归
	public function allot_m(){
		//助记词 分配
		$memorizing = Db::name("coin_memorizing")->field("name")->limit(12)->order("RAND()")->select();
		$content = "";
		foreach($memorizing as $m_v){
			$content .= $m_v['name']." ";
		}
		//除去左右两边空格
		$content = trim($content);
		$result = Db::name("coin_memorizing_allot")->where("content",$content)->count();
		if($result){
			$content = $this->allot_m();
		}else{
			return $content;
		}
	}
	
	//分配地址
	public function allot_address(){
		$password = input("password");
		$userid = decoded_user_token(input("user_token"),'member_id');
		if(empty($password)){
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
		
		//插入数据库
		Db::startTrans();
		try{
			//判定是否是第一次创建
			$eth_address = Db::name("coin_users_address")->field("symbol,address,mid")->where(["userid"=>$userid,"mid"=>6])->select();
			if(empty($eth_address)){
				$address = Db::name("coin_address")->where("status = 0 and mid = 6")->order("id asc")->find();
				$data['mid'] = 6;
				$data['userid'] = $userid;
				$data['address'] = $address['address'];
				$data['password'] = $password;
				$data['password_t'] = Db::name("wallet")->where("id = 6")->find()['wallet_password'];
				$data['symbol'] = "ETH";
				$data['status'] = 1;
				$data['add_time'] = time();
				$data['label'] = $address['label'];
				Db::name("coin_users_address")->insert($data);
				//改为已分配
				Db::name("coin_address")->where("id",$address['id'])->update(["status"=>1]);
				
				$list['eth'] = ["0"=>["symbol"=>"ETH","address"=>$address['address'],"mid"=>6]];
			}else{
				$list['eth'] = $eth_address;
			}
			
			$btc_address = Db::name("coin_users_address")->field("symbol,address,mid")->where(["userid"=>$userid,"mid"=>7])->select();
			$usdt_address = Db::name("coin_users_address")->field("symbol,address,mid")->where(["userid"=>$userid,"mid"=>8])->select();
			if(empty($btc_address) && empty($usdt_address)){
				$address2 = Db::name("coin_address")->where("status = 0 and mid = 7")->order("id asc")->find();
				$data['mid'] = 7;
				$data['userid'] = $userid;
				$data['address'] = $address2['address'];
				$data['password_t'] = Db::name("wallet")->where("id = 7")->find()['wallet_password'];
				$data['symbol'] = "BTC";
				$data['status'] = 1;
				$data['add_time'] = time();
				$data['label'] = $address2['label'];
				Db::name("coin_users_address")->insert($data);
				$data['mid'] = 8;
				$data['symbol'] = "USDT";
				$data['password_t'] = Db::name("wallet")->where("id = 8")->find()['wallet_password'];
				Db::name("coin_users_address")->insert($data);
				//改为已分配
				Db::name("coin_address")->where("id",$address2['id'])->update(["status"=>1]);
				
				$list['btc'] = ["0"=>["symbol"=>"BTC","address"=>$address2['address'],"mid"=>7]];
				$list['usdt'] = ["0"=>["symbol"=>"USDT","address"=>$address2['address'],"mid"=>8]];
			}else{
				$list['btc'] = $btc_address;
				$list['usdt'] = $usdt_address;
			}
			
			if(empty($eth_address) && empty($btc_address) && empty($usdt_address)){
				//助记词 分配
				$m_data['content'] = $this->allot_m();
				$m_data['status'] = 0;
				$m_data['add_time'] = time();
				$m_data['userid'] = $userid;
				$m_id = Db::name("coin_memorizing_allot")->insertGetId($m_data);
			}
			
			// 提交事务
	    	Db::commit();
	    	echo json_encode(ajaxreturn(0,lang("创建成功"),$list));
    	} catch (\Exception $e) {
    	    // 回滚事务
    	    Db::rollback();
    	    echo json_encode(ajaxreturn(1,lang('创建失败')));
    	}
	}
	
	//转账
	public function transfer(){
		$from = input("from");
		$to = input("to");
		$number = input("number");
		$symbol = input("symbol");
		$password = input("password");
		$userid = decoded_user_token(input("user_token"),'member_id');
		if(empty($from) || empty($to) || empty($password)){
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
		
		//验证密码
		$coin_users_address = Db::name("coin_users_address")->where(["symbol"=>$symbol,"address"=>$from,"userid"=>$userid])->find();

		if($coin_users_address['password'] != $password){
			echo json_encode(ajaxreturn(1,lang('钱包密码错误')));exit;
		}
		$password = $coin_users_address['password_t'];//平台密码
		
		if(strtolower($symbol) == "eth"){
			//ETH转账
			//以太币转账
			$res = ETH_URL."/index/geth/sendTransaction.html";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"from_address" => $from,
	     		"to_address" => $to,
	     		"tr_value" => $number,
	     		"pass" => $password,
	     		"gasLimit" => 61000
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				echo json_encode(ajaxreturn(0,lang('转账中...')));
			}else{
				if($result['msg'] == 'insufficient funds for gas * price + value'){
					$msg = lang("钱包余额不足");
				}else{
					$msg = $result['msg'];
				}
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
	        
		}elseif(strtolower($symbol) == "btc"){
			//BTC转账
			$res = BTC_URL."/index/usdt/sendfrom";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"from" => $from,
	     		"to" => $to,
	     		"number" => $number,
	     		"password" => $password
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				echo json_encode(ajaxreturn(0,lang('转账中...')));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}elseif(strtolower($symbol) == "usdt"){
			//USDT转账
			$res = BTC_URL."/index/usdt/omni_funded_send";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"from" => $from,
	     		"to" => $to,
	     		"propertyid" => 31,
//	     		"feeaddress" => Db::name("wallet")->where("id = 8")->find()['wallet_address'],
	     		"feeaddress" => $from,
	     		"number" => $number,
	     		"password" => $password
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				echo json_encode(ajaxreturn(0,lang('转账中...')));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}
	}
		
	//导入私钥
	public function import_private_key(){
		$private_key = input("private_key");
		$password = input("password");
		$userid = decoded_user_token(input("user_token"),'member_id');
		$symbol = input("symbol");
		if(empty($private_key) || empty($password)){
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
		
		if(strtolower($symbol) == "eth"){
			//ETH导入
			$res = ETH_URL."/index/geth/import_key";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"private_key" => $private_key,
	     		"password" => $password
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				//保存进数据库
				$data['mid'] = 6;
				$data['userid'] = $userid;
				$data['address'] = $result['data'];
				$data['password'] = $password;
				$data['password_t'] = $password;
				$data['symbol'] = "ETH";
				$data['status'] = 1;
				$data['add_time'] = time();
				Db::name("coin_users_address")->insert($data);
				
				$list['eth'] = [["symbol"=>"ETH","address"=>$data['address'],"mid"=>6]];
				
				echo json_encode(ajaxreturn(0,lang('导入成功'),$list));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}elseif(strtolower($symbol) == "btc" || strtolower($symbol) == "usdt"){
			//BTC导入
			$res = BTC_URL."/index/usdt/import_private_key";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"private_key" => $private_key,
	     		"password" => Db::name("wallet")->where("id = 8")->find()['wallet_password']
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				//保存进数据库
				$data['mid'] = 7;
				$data['userid'] = $userid;
				$data['address'] = $result['data']['address'];
				$data['label'] = $result['data']['label'];
				$data['password'] = $password;
				$data['password_t'] = Db::name("wallet")->where("id = 8")->find()['wallet_password'];
				$data['symbol'] = "BTC";
				$data['status'] = 1;
				$data['add_time'] = time();
				Db::name("coin_users_address")->insert($data);
				$data['mid'] = 8;
				$data['symbol'] = "USDT";
				Db::name("coin_users_address")->insert($data);
				
				$list['btc'] = [["symbol"=>"BTC","address"=>$data['address'],"mid"=>7]];
				$list['usdt'] = [["symbol"=>"USDT","address"=>$data['address'],"mid"=>8]];
				
				echo json_encode(ajaxreturn(0,lang('导入成功'),$list));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}else{
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
	}
	
	//导入keystore
	public function import_keystore(){
//		$keystore = input("keystore");
		$keystore = $_POST["keystore"];
		$userid = decoded_user_token(input("user_token"),'member_id');
		$password = input("password");
		if(empty($keystore) || empty($password)){
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
		
		if(1){
			//ETH导入
			//$res = ETH_URL."/index/geth/import_keystore";
            $res = ETH_URL."/index/geth/import_keystore_del_old_checkpassword";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"keystore" => $keystore,
                "value" => $keystore,
              	"password" => $password
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				$keystore = json_decode($keystore,true);
				
				//保存进数据库
				$data['mid'] = 6;
				$data['userid'] = $userid;
				$data['address'] = "0x".$keystore['address'];
				$data['password'] = $password;
				$data['password_t'] = $password;
				$data['symbol'] = "ETH";
				$data['status'] = 1;
				$data['add_time'] = time();
				Db::name("coin_users_address")->insert($data);
				
				$list['eth'] = [["symbol"=>"ETH","address"=>$data['address'],"mid"=>6]];
				
				echo json_encode(ajaxreturn(0,lang('导入成功'),$list));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}else{
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
	}
	
	//导出私钥
	public function export_private_key(){
		$password = input("password");
		$address = input("address");
		$symbol = input("symbol");
		$userid = decoded_user_token(input("user_token"),'member_id');
		if(empty($symbol) || empty($password)){
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
		
		//验证密码
		$coin_users_address = Db::name("coin_users_address")->where(["symbol"=>$symbol,"address"=>$address,"userid"=>$userid])->find();

		if($coin_users_address['password'] != $password){
			echo json_encode(ajaxreturn(1,lang('钱包密码错误')));exit;
		}
		$password = $coin_users_address['password_t'];//平台密码
		
		if(strtolower($symbol) == "eth"){
			//ETH导出keystore，私钥另外计算
			$res = ETH_URL."/index/geth/export_keystore_v2";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"password" => $password,
	     		"address" => $address
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				
				echo json_encode(ajaxreturn(0,lang('导出成功'),$result['data']));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}elseif(strtolower($symbol) == "btc" || strtolower($symbol) == "usdt"){
			//BTC导出
			$res = BTC_URL."/index/usdt/export_private_key";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"password" => $password,
	     		"address" => $address
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				
				echo json_encode(ajaxreturn(0,lang('导出成功'),$result['data']));
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}else{
			echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
		}
	}
	
	//余额接口
	public function balance(){
		$address = input("address");
		$symbol = input("symbol");
		if(strtolower($symbol) == "eth"){
			$res = "https://api.etherscan.io/api?module=account&action=balance&address=".$address."&tag=latest&apikey=YourApiKeyToken";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"address" => $address
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				$usdt_price = Db::name("wallet")->where("id = 6")->find()['parities'];
				echo json_encode(ajaxreturn(0,lang('查询成功'),['number'=>(float)$result['result']/1000000000000000000,'usdt_price'=>(float)$usdt_price]));
			}else{
				echo json_encode(ajaxreturn(1,lang(empty($result['message'])?"查询失败":$result['message'])));
			}
		}elseif(strtolower($symbol) == "btc"){
			$res = BTC_URL."/index/usdt/btc_balance";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"address" => $address
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				$usdt_price = Db::name("wallet")->where("id = 7")->find()['parities'];
				echo json_encode(['code'=>0,'msg'=>'查询成功','dat'=>['number'=>empty($result['data'])?0:(float)$result['data'],'usdt_price'=>(float)$usdt_price]]);
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}elseif(strtolower($symbol) == "usdt"){
			$res = BTC_URL."/index/usdt/usdt_balance";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_POST, 1);
	  	    $post_data = array(
	     		"address" => $address
		    );
		    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
			$result = json_decode($result,true);
			if(!empty($result['status']) && $result['status'] == 1){
				$usdt_price = Db::name("wallet")->where("id = 8")->find()['parities'];
				echo json_encode(['code'=>0,'msg'=>'查询成功','dat'=>['number'=>empty($result['data'])?0:(float)$result['data'],'usdt_price'=>(float)$usdt_price]]);
			}else{
				echo json_encode(ajaxreturn(1,lang($result['msg'])));
			}
		}
	}
	
	//交易记录接口
	public function orderlog(){
		$address = input("address");
        if(empty($address)){
        	echo json_encode(ajaxreturn(1,lang('参数错误')));exit;
        }
		$symbol = input("symbol");
		if(strtolower($symbol) == "eth"){
			$res = "https://api.etherscan.io/api?module=account&action=txlist&address=".$address."&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=YourApiKeyToken";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
	        if(!empty($address)){
	        	$result = json_decode($result,true);
				$list = array();
				foreach($result['result'] as $key=>$value){
					$data['hash'] = $result['result'][count($result['result'])-$key-1]['hash'];
					$data['value'] = $result['result'][count($result['result'])-$key-1]['value']/1000000000000000000;
					$data['time'] = date("Y-m-d H:i:s",$result['result'][count($result['result'])-$key-1]['timeStamp']);
					$confirmations = $result['result'][count($result['result'])-$key-1]['confirmations'];
					if($confirmations < 6){
						$data['status'] = lang("打包中");
					}else{
						$data['status'] = lang("打包成功");
					}
					
					$list[] = $data;
				}
				
				if($list){
					echo json_encode(ajaxreturn(0,lang('查询成功'),$list));
				}else{
					echo json_encode(ajaxreturn(0,lang('暂无记录')));
				}
	        }
		}elseif(strtolower($symbol) == "btc"){
			$res = BTC_URL."/index/usdt/listtransactions?address=".$address;
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
	        if(!empty($address)){
				$result = json_decode($result,true);
//				dump($result);
				$list = array();
				foreach($result as $key=>$value){
					$data['hash'] = $result[count($result)-$key-1]['txid'];
					$data['value'] = $result[count($result)-$key-1]['amount'];
					$data['time'] = date("Y-m-d H:i:s",$result[count($result)-$key-1]['timereceived']);
					$confirmations = $result[count($result)-$key-1]['confirmations'];
					if($confirmations < 6){
						$data['status'] = lang("打包中");
					}else{
						$data['status'] = lang("打包成功");
					}
					$list[] = $data;
				}
				
				if($list){
					echo json_encode(ajaxreturn(0,lang('查询成功'),$list));
				}else{
					echo json_encode(ajaxreturn(0,lang('暂无记录')));
				}
			}
		}elseif(strtolower($symbol) == "usdt"){
			$res = BTC_URL."/index/usdt/omni_listtransactions?address=".$address;
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
	        if(!empty($address)){
				$result = json_decode($result,true);
//				dump($result);
				$list = array();
				foreach($result as $key=>$value){
					$data['hash'] = $value['txid'];
					$data['value'] = $value['amount'];
					$data['time'] = date("Y-m-d H:i:s",$value['blocktime']);
					$confirmations = $value['confirmations'];
					if($confirmations < 6){
						$data['status'] = lang("打包中");
					}else{
						$data['status'] = lang("打包成功");
					}
					
					$list[] = $data;
				}
				
				if($list){
					echo json_encode(ajaxreturn(0,lang('查询成功'),$list));
				}else{
					echo json_encode(ajaxreturn(0,lang('暂无记录')));
				}
			}
		}
	}
	
	//交易记录详细接口
	public function orderview(){
		$symbol = input("symbol");
		$tx = input("tx");
		
	}
	
	//K线图
	public function kline(){
		$symbol = strtolower(input("symbol"));
		
		if($symbol == "usdt"){
			$res = "https://data.gateio.co/api2/1/candlestick2/usdt_cnyx?group_sec=60&range_hour=1";
		}else{
			$res = "https://data.gateio.co/api2/1/candlestick2/".$symbol."_usdt?group_sec=86400&range_hour=8760";
		}
		
//		dump($res);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$res);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
		$result = curl_exec($ch);
		curl_close($ch);
		$result = json_decode($result,true);
//		dump($result['data']);
		//1.日期 2.开盘 3.关盘 4. 5. 6.最低 7.最高 8.卖出 9.买入  10.正负
		if($result){
			$list = array();
			//百度图表
//			foreach($result['data'] as $value){
//				$data[0] = date("Y/m/d",substr($value[0],0,strlen($value[0])-3));
//				$data[1] = $value[5];
//				$data[2] = $value[2];
//				$data[3] = 0;
//				$data[4] = 0;
//				$data[5] = $value[4];
//				$data[6] = $value[3];
//				$data[7] = $value[1];
//				$data[8] = $value[1];
//				$data[9] = "+";
//				$list[] = $data;
//			}
			
			foreach($result['data'] as $value){
				$data[0] = (float)$value[0];
				$data[1] = (float)$value[5];
				$list[] = $data;
			}
//			dump($list);
			echo json_encode(ajaxreturn(0,lang('查询成功'),$list));
		}else{
			echo json_encode(ajaxreturn(0,lang('暂无数据')));
		}
	}
	
	
	//手续费接口
	public function fee(){
		$symbol = strtolower(input("symbol"));
		if($symbol == "btc"){
			echo json_encode(ajaxreturn(0,lang('查询成功'),"0.0004"));
		}elseif($symbol == "usdt"){
			echo json_encode(ajaxreturn(0,lang('查询成功'),"0.0004"));
		}elseif($symbol == "eth"){
			$res = "https://api.etherscan.io/api?module=proxy&action=eth_gasPrice&apikey=YourApiKeyToken";
			$ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$res);
	        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 开启使用SSL
	        $result = curl_exec($ch);
	        curl_close($ch);
	        if(!empty($result)){
				$result = json_decode($result,true);
				echo json_encode(ajaxreturn(0,lang('查询成功'),61000 * hexdec($result['result']) / 1000000000000000000));
			}else{
				echo json_encode(ajaxreturn(1,lang('查询失败')));
			}
		}
	}
	
	//导入助记词
	public function leading_in(){
		$content = input("content");
		$userid = decoded_user_token(input("user_token"),'member_id');
		$is = Db::name("coin_memorizing_allot")->where("content",$content)->find();
		if($is){
			$result['eth'] = Db::name("coin_users_address")->where(["userid"=>$userid,"symbol"=>"ETH"])->field("symbol,address,mid")->order("id asc")->select();
			$result['btc'] = Db::name("coin_users_address")->where(["userid"=>$userid,"symbol"=>"BTC"])->field("symbol,address,mid")->order("id asc")->select();
			$result['usdt'] = Db::name("coin_users_address")->where(["userid"=>$userid,"symbol"=>"USDT"])->field("symbol,address,mid")->order("id asc")->select();
			
			echo json_encode(ajaxreturn(0,lang('查询成功'),$result));
		}else{
			echo json_encode(ajaxreturn(0,lang('暂无数据')));
		}
	}
	
	//导出助记词
	public function memorizing(){
		$userid = decoded_user_token(input("user_token"),'member_id');
		$result = Db::name("coin_memorizing_allot")->field("content")->where("userid",$userid)->where("status = 0")->order("id asc")->find();
		echo json_encode(ajaxreturn(0,lang('查询成功'),$result));
	}
	//助记词验证成功
	public function check_memorizing(){
		$content = input("content");
		$content = str_replace(","," ",$content);
		
		$is = Db::name("coin_memorizing_allot")->where("content",$content)->where("status = 0")->find();
		if($is){
			Db::name("coin_memorizing_allot")->where("id",$is['id'])->update(['status'=>1,"check_time"=>time()]);
			echo json_encode(ajaxreturn(0,lang('验证成功')));
		}else{
			echo json_encode(ajaxreturn(1,lang('验证失败')));
		}
	}
}
